<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="128px"
    >
      <el-form-item label="订单编号" prop="orderIdEq">
        <el-input
          v-model="queryParams.orderIdEq"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="主订单号" prop="mainOrderIdEq">
        <el-input
          v-model="queryParams.mainOrderIdEq"
          placeholder="请输入主订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="目的港" prop="unloadingPortEq">
        <el-input
          v-model="queryParams.unloadingPortEq"
          placeholder="请输入目的港"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="客户公司" align="center" prop="companyName" />
      <el-table-column label="运单号" align="center" prop="mainOrderId" width="150" />
      <el-table-column label="订单号" align="center" prop="orderId" width="100" />
      <el-table-column label="目的港" align="center" prop="destinationCityName" />
      <el-table-column label="件数" align="center" prop="warehouseNumber" width="60"/>
      <el-table-column label="计划" align="center" prop="planStatus" :formatter="planStatusFormat" width="150"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" width="150"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            v-show="scope.row.status == 1"
            type="primary"
            @click="rucang(scope.row.id)"
            v-hasPermi="['business:cargo-info:receipt']"
          >
            入仓
          </el-button>
          <el-button
            link
            v-show="scope.row.status == 2"
            type="primary"
            @click="baozhuang(scope.row.id)"
            v-hasPermi="['business:cargo-info:pack']"
          >
            包装
          </el-button>
          <el-button
            link
            type="primary"
            v-show="scope.row.status == 3 && scope.row.packStatus==0"
            @click="packCheckFuc(scope.row)"
            v-hasPermi="['business:cargo-info:packCheck']"
          >
            包装审核
          </el-button>
          <el-button
            link
            v-show="scope.row.status == 3 && scope.row.planStatus==2"
            type="primary"
            @click="chuku(scope.row.id)"
            v-hasPermi="['business:cargo-info:shipment']"
          >
            出仓
          </el-button>
          <el-button
            link
            v-show="scope.row.status == 4 && scope.row.planStatus == 2"
            type="primary"
            @click="anjian(scope.row.id)"
            v-hasPermi="['business:cargo-info:scene']"
          >
            安检
          </el-button>
          <el-button
            link
            type="primary"
             v-show="scope.row.status == 5"
            @click="baoguan(scope.row.id)"
            v-hasPermi="['business:cargo-info:customs']"
          >
            报关
          </el-button>
          <el-button
            link
            v-show="scope.row.status == 6"
            type="primary"
            @click="jiaodan(scope.row.id)"
            v-hasPermi="['business:cargo-info:handUp']"
          >
            交单
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm(scope.row.id)"
          >
            订单信息
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <CargoView ref="CargoViewRef"/>
  <ReceiptForm ref="ReceiptFormRef" @success="getList"/>
  <PackForm ref="PackFormRef" @success="getList"/>
  <PackCheckForm ref="PackCheckFormRef" @success="getList" />
  <ShipmentForm ref="ShipmentFormRef" @success="getList"/>
  <SceneForm ref="SceneFormRef" @success="getList"/>
  <CustomsForm ref="CustomsFormRef" @success="getList"/>
  <SubmitForm ref="SubmitFormRef" @success="getList"/>
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import { CargoInfoApi, CargoInfoVO } from '@/api/business/cargoinfo'
import { dateFormatter } from '@/utils/formatTime'
import CargoView from './cargoView.vue'
import ReceiptForm from '../receipt/ReceiptForm.vue'
import PackForm from '../receipt/PackForm.vue'
import PackCheckForm from '../cargoinfo/packCheckForm.vue'
import ShipmentForm from '../receipt/shipmentForm.vue'
import SceneForm from '../scene/sceneForm.vue'
import CustomsForm from '../customs/CustomsForm.vue'
import SubmitForm from '../scene/submitForm.vue'

/** 货物资料 列表 */
defineOptions({ name: 'CargoManageIndex' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(false) // 列表的加载中
const list = ref<CargoInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderIdEq: undefined,
  mainOrderIdEq: undefined,
  unloadingPortEq: undefined,
  status: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  if(!queryParams.orderIdEq &&  !queryParams.mainOrderIdEq && !queryParams.unloadingPortEq){
    return;
  }
  loading.value = true
  try {
    const data = await CargoInfoApi.getCargoInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const planStatusFormat = (row, index, cell)=>{
  if(cell == 0){
    return "";
  }else if(cell == 1){
    return "待下发计划"
  }else if(cell == 2){
    return "计划中"
  }else if(cell == 3){
    return "计划完成"
  }else{
    return "";
  }
}

const statusFormat = (row, index, cell)=>{
  if(cell == 1){
    return "待入仓";
  }else if(cell == 2){
    return "待包装"
  }else if(cell == 3){
    return "待出仓"
  }else if(cell == 4){
    return "待安检"
  }else if(cell == 5){
    return "待报关"
  }else if(cell == 6){
    return "待交单"
  }else if(cell == 7){
    return "运输中"
  }else if(cell == 8){
    return "已完成"
  }else{
    return "";
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const CargoViewRef = ref()
const openForm = (id?: number) => {
  CargoViewRef.value.open(id)
}

const ReceiptFormRef = ref();
const rucang = (id:number)=>{
  ReceiptFormRef.value.open(id);
}

const PackFormRef = ref();
const baozhuang = (id:number)=>{
  PackFormRef.value.open(id);
}

const PackCheckFormRef = ref();
const packCheckFuc = (row)=>{
    PackCheckFormRef.value.open(row.id);
}

const ShipmentFormRef = ref();
const chuku = (id)=>{
  ShipmentFormRef.value.open(id);
}

const SceneFormRef = ref();
const anjian = (id:number)=>{
  SceneFormRef.value.open(id);
}

const CustomsFormRef = ref();
const baoguan = (id:number)=>{
  CustomsFormRef.value.open(id);
}

const SubmitFormRef = ref();
const jiaodan = (id:number)=>{
  SubmitFormRef.value.open(id);
}

/** 初始化 **/
onMounted(() => {
  // getList()
})
</script>
